package com.tedu.day07springmvc.controller;

import com.tedu.day07springmvc.mapper.UserMapper;
import com.tedu.day07springmvc.pojo.dto.UserDTO;
import com.tedu.day07springmvc.pojo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;
//    @RequestMapping("/regUser")
//    @ResponseBody
//    public String regUser(HttpServletRequest request) {
//        System.out.println("开始处理用户注册！！！");
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        String nickname = request.getParameter("nickname");
//        String ageStr = request.getParameter("age");
//        int age = Integer.parseInt(ageStr);
//        System.out.println(username+","+password+","+nickname+","+age);
//        return "注册成功";
//    }

//    @RequestMapping("/regUser")
//    @ResponseBody
//    public String regUser(String username,String password,String nickname,int age) {
//        System.out.println("注册信息:"+username+","+password+","+nickname+","+age);
//        return "注册成功";
//    }
    @RequestMapping("/regUser")
    @ResponseBody
    public String regUser(UserDTO userDTO) {
        System.out.println(userDTO);
        User user = new User();
        user.setUsername(userDTO.getUsername());
        user.setPassword(userDTO.getPassword());
        user.setNickname(userDTO.getNickname());
        user.setAge(userDTO.getAge());
        user.setCreated(new Date());
        int num=userMapper.insertUser(user);
        return num>0?"注册成功":"注册失败";
    }
}
